#include <windows.h>
#include <stdio.h>

int main()
{ 
    HANDLE hSlot = CreateMailslotW(L"\\\\.\\mailslot\\qt4\\dbzhang800", 0, MAILSLOT_WAIT_FOREVER, NULL);

    if (hSlot == INVALID_HANDLE_VALUE) { 
        printf("CreateMailslot failed with %d\n", (int)GetLastError());
        return -1; 
    } else {
        printf("Mailslot created successfully.\n");
        while (1) {
            DWORD cbMsg = 0;
            DWORD cMsg = 0;
            if (GetMailslotInfo(hSlot, NULL, &cbMsg, &cMsg, NULL)) {
                if (cbMsg == MAILSLOT_NO_MESSAGE) {
                    //printf("no message\n");
                    continue;
                }
                if (cMsg){
                    printf("has message\n");
                    char * buf = new char[cbMsg+1];
                    DWORD cbRead = 0;
                    ReadFile(hSlot, buf, cbMsg, &cbRead, NULL); 
                    buf[cbMsg] = 0;
                    printf("------------\n%s %d\n-------------\n", buf, (int)cbRead);
                    delete [] buf;
                }
            }
        }
    }
    return 0;
}

